#!/bin/bash
#### Usage: sh dbRestore.sh FILEPATH DBNAME
usage ()
{
	echo "Usage: sh $0  <mysql|mongo|cassandra> <db-backup-path> [database-name|keyspace-name]"
	echo "db-backup-path - For mysql, give dump file path. For mongo, give the path for folder that contains DB backup"
	echo "database-name - Mysql database name to which backup contents to be added. For mongo, this is optional field and require only if you need to restore a specific database"
}

# Check we're running as root
if [ "$(id -u)" != "0" ]; then
  echo "The restore script must be run as root"
  exit 1
fi

db_type=$1
if [ $# -eq 3 ] && [ "$db_type" = "mongo" ]
then
	edir=`basename $2`;
	if [ "$edir" != "$3" ]
	then
		echo "Please provide complete path for the mongo database."
		exit 1
	fi
fi

if [ $# -eq 3 ] && [ "$db_type" = "cassandra" ]
then
	if [ ! -d "$2/$3" ]
	then
		echo "Please locate parent directory for the provided keyspace - $3."
		exit 1
	fi
fi

if [ "$db_type" = "mysql" ]
then
	if [ $# != 3 ]
	then
		usage
		exit
	fi
	
	CAT="/bin/cat"
	file=$2;
	db=$3;
	msp="msp"
	msp_report="msp_report"
	msp_mon="msp_mon"
	if [ "$db" = "msp" ] || [ "$db" = "$msp_report" ] || [ "$db" = "$msp_mon" ]; then
		SQL_FILE=$(echo "$file" | sed -e 's/[[:space:]]*//')
		fromdos $SQL_FILE
		$CAT $SQL_FILE | /usr/bin/mysql --f --user=root --password=xAVa6Aze $db
		if [ $? -ne 0 ]; then
			echo "Failed to install the db schema "
		else
			echo "Successfully restored the db schema"
		fi
	else
		echo "Invalid DB"
		exit;
	fi
elif [ "$db_type" = "mongo" ]
then
	if [ $# -lt 2 ]
	then
		usage
		exit
	fi
	BACKUP_DIR=$2;
	BACKUP_DB=$3;
	if [ ! -d "$BACKUP_DIR" ]
	then
		echo "Backup directory ${BACKUP_DIR} does not exist."
		exit 1
	fi

	if [ ! -z "$BACKUP_DB" ]
	then
		mongorestore --drop $BACKUP_DIR --db $BACKUP_DB
		if [ $? -eq 0 ]; then
			echo "Restored the mongo database ${BACKUP_DB} successfully"
		else
			echo "Failed to restore mongo database - $BACKUP_DB"
		fi
	else
		mongorestore --drop $BACKUP_DIR
		if [ $? -eq 0 ]; then
			echo "Restored the mongo database ${BACKUP_DB} successfully"
		else
			echo "Failed to restore entire mongo database"
		fi
	fi
elif [ "$db_type" = "cassandra" ]
then
	if [ $# -lt 2 ]
	then
		usage
		exit
	fi
	BACKUP_DIR=$2;
	BACKUP_KEYSPACE=$3;
	if [ ! -d "$BACKUP_DIR" ]
	then
		echo "Backup directory ${BACKUP_DIR} does not exist."
		exit 1
	fi
	ls $BACKUP_DIR/*/*/*db  >/dev/null 2>&1
	if [ $? -ne 0 ]; then
		echo "Please provide proper Cassandra restore path"
		exit 1
	fi
	
	ls $BACKUP_DIR/schema.txt  >/dev/null 2>&1
	if [ $? -ne 0 ]; then
		echo "Please provide proper Cassandra restore path"
		exit 1
	fi
	echo "Stopping cassandra..."
	service cassandra stop
	sleep 5
	echo "Removing files..."
	rm -fr /var/lib/cassandra/data/* /var/lib/cassandra/commitlog/* /var/lib/cassandra/saved_caches/*
	#rm -fr /var/lib/cassandra/data/.* /var/lib/cassandra/commitlog/.* /var/lib/cassandra/saved_caches/.*
	echo "Starting cassandra..."
	service cassandra start
	sleep 15
	i="0"
	is_running=false
	while [ $i -lt 3 ]
	do
		echo "status check $i"
		service cassandra status
		if [ $? -ne 0 ]; then
	   		sleep 15
		else
	   		is_running=true
	   		break
		fi
		i=$[$i+1]
	done
	
	if [ "$is_running" = false ] ; then
    	echo "Cassandra service start failed, exiting after 3 retries .."
   		exit 1
	fi

	echo "Deploying schema $BACKUP_DIR/schema.txt ..."
	cqlsh -ucassandra -pcassandra -f "$BACKUP_DIR/schema.txt"
	if [ $? -ne 0 ]; then
	   	echo "Schema Deploy failed, please re-run restore script."
	   	exit 1
	fi
	if [ ! -z "$BACKUP_KEYSPACE" ]
	then
		echo "Restoring Cassandra keyspace - $BACKUP_KEYSPACE from $BACKUP_DIR ..."
		for LINE in `/bin/ls $BACKUP_DIR/$BACKUP_KEYSPACE/* -d`
		do
		    LINE=$(echo "$LINE" | sed -e 's/[[:space:]]*//')
		    DEST=$(echo "$LINE" | sed -e "s:$BACKUP_DIR:/var/lib/cassandra/data/:g")
		    if [ ! -d "$DEST" ]; then
		    	mkdir -p $DEST
		    fi
		    #rm -fr $DEST/*
			cp -fr $LINE/* $DEST >/dev/null 2>&1
		done
	else
		echo "Restoring Cassandra node from $BACKUP_DIR ..."
		for LINE in `/bin/ls $BACKUP_DIR/*/* -d`
		do
		    LINE=$(echo "$LINE" | sed -e 's/[[:space:]]*//')
		    DEST=$(echo "$LINE" | sed -e "s:$BACKUP_DIR:/var/lib/cassandra/data/:g")
		    if [ ! -d "$DEST" ]; then
		    	mkdir -p $DEST
		    fi
		    #rm -fr $DEST/*
		    cp -fr $LINE/* $DEST >/dev/null 2>&1
		done
	fi
	chown 644 /var/lib/cassandra/data/ -R
	chown cassandra.cassandra /var/lib/cassandra/data/ -R
	echo "Cassandra restore ${BACKUP_DB} successfully completed."
	service cassandra stop
	sleep 5
	service cassandra start
	sleep 10
	if [ $? -eq 0 ]; then
		echo "Cassandra service started after restore."
	else
		echo "Failed to start cassandra service after restore."
	fi
else
	echo "Provide MySQL / MongoDB / Cassandra as Database type."
	usage
	exit
fi
